package cn.stylefeng.guns.modular.hx.basic.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.hx.basic.entity.HxBasicPositionGoods;
import cn.stylefeng.guns.modular.hx.basic.mapper.HxBasicPositionGoodsMapper;
import cn.stylefeng.guns.modular.hx.basic.model.params.HxBasicPositionGoodsParam;
import cn.stylefeng.guns.modular.hx.basic.model.result.HxBasicPositionGoodsResult;
import  cn.stylefeng.guns.modular.hx.basic.service.HxBasicPositionGoodsService;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageStockParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStorageStockResult;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 库位商品关联表 服务实现类
 * </p>
 *
 * @author zhucx
 * @since 2023-09-06
 */
@Service
public class HxBasicPositionGoodsServiceImpl extends ServiceImpl<HxBasicPositionGoodsMapper, HxBasicPositionGoods> implements HxBasicPositionGoodsService {

    @Override
    public void add(HxBasicPositionGoodsParam param){
        HxBasicPositionGoods entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(HxBasicPositionGoodsParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(HxBasicPositionGoodsParam param){
        HxBasicPositionGoods oldEntity = getOldEntity(param);
        HxBasicPositionGoods newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public HxBasicPositionGoodsResult findBySpec(HxBasicPositionGoodsParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<HxBasicPositionGoodsResult> findListBySpec(HxBasicPositionGoodsParam param){
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(HxBasicPositionGoodsParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(HxBasicPositionGoodsParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private HxBasicPositionGoods getOldEntity(HxBasicPositionGoodsParam param) {
        return this.getById(getKey(param));
    }

    private HxBasicPositionGoods getEntity(HxBasicPositionGoodsParam param) {
        HxBasicPositionGoods entity = new HxBasicPositionGoods();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public HxStorageStockResult findStorageStockBySpec(HxStorageStockParam param) {
		return this.baseMapper.findStorageStockBySpec(param);
	}

	@Override
	public boolean saveBatchList(List<HxBasicPositionGoodsParam> list) {
		return this.baseMapper.saveBatchList(list);
	}

    @Override
    public void deleteByOne(HxBasicPositionGoodsParam hxBasicPositionGoodsParam) {
        LambdaQueryWrapper<HxBasicPositionGoods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<HxBasicPositionGoods> wrapper = lambdaQueryWrapper
                .eq(HxBasicPositionGoods::getPositionId, hxBasicPositionGoodsParam.getPositionId())
                .eq(HxBasicPositionGoods::getGoodsId, hxBasicPositionGoodsParam.getGoodsId());
        this.baseMapper.delete(wrapper);
    }

	@Override
	public void empty() {
		List<HxBasicPositionGoodsResult> list = this.findemptyListBySpec();
		for (HxBasicPositionGoodsResult r : list) {
			this.baseMapper.deleteById(r.getId());
		}
	}

    @Override
    public void removeEmpty(List<Long> warehouseIdList) {
        List<HxBasicPositionGoodsResult> hxBasicPositionGoodsResults = this.baseMapper.findemptyBywarehouseIdList(warehouseIdList);
        for(HxBasicPositionGoodsResult obj : hxBasicPositionGoodsResults){
            this.baseMapper.deleteById(obj.getId());
        }
    }

    @Override
	public List<HxBasicPositionGoodsResult> findemptyListBySpec() {
		return this.baseMapper.findemptyListBySpec();
	}

}
